package com.xu.controller;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.RandomUtil;
import com.xu.mapper.EvaluateMapper;
import com.xu.mapper.ImgMapper;
import com.xu.mapper.ProductMapper;
import com.xu.mapper.VouchersMapper;
import com.xu.pojo.*;
import com.xu.service.OrderService;
import com.xu.service.ProductService;
import com.xu.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import static cn.hutool.core.img.ImgUtil.toBufferedImage;

@Controller
public class ProductController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private ProductService productService;

    @Autowired
    private StockService stockService;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ImgMapper imgMapper;

    @Autowired
    private VouchersMapper vouchersMapper;

    @Autowired
    private EvaluateMapper evaluateMapper;

    @RequestMapping("/queryAllProduct")
    public String queryAllProduct(Model model)
    {
        List<Product> list = productService.queryAllProduct();
        model.addAttribute("product", list);
        return "admin/product/product";
    }

    @RequestMapping("/toProductAdd")
    public String toProductAdd(Model model)
    {
        List<Img> imgs = imgMapper.queryAllImg();
        model.addAttribute("imgs", imgs);
        return "admin/product/product-add";
    }

    @RequestMapping("/productAdd")
    public String productAdd(Product product)
    {
        productService.addProduct(product);
        return "redirect:/queryAllProduct";
    }

    @RequestMapping("/productDel/{id}")
    public String delProduct(@PathVariable("id") int id,Model model)
    {
        System.out.println("===================================================="+id);
        if (orderService.queryOrderById(id)==null)
        {
            System.out.println(orderService.queryOrderById(id));
            productService.delProduct(id);
            return "redirect:/queryAllProduct";
        }
        else
        {
            model.addAttribute("msg", "该产品已有订单/库存，无法删除");
            return "admin/product/product-error";
        }

    }

    @RequestMapping("/toProductUpd/{id}")
    public String productUpd(@PathVariable("id") int id,Model model)
    {
        Product product = productService.queryProductById(id);
        System.out.println("====================================="+product);
        model.addAttribute("product", product);
        return "admin/product/product-upd";
    }

    @RequestMapping("/toProductDetail/{id}")
    public String toProductDetail(HttpSession httpSession, @PathVariable("id") int id, Model model,HttpServletResponse response)
    {
        try {
            StpUtil.checkLogin();
            Product product = productService.queryProductById(id);
            Stock stock = stockService.queryStockById(id);
            List<Evaluate> evaluates = evaluateMapper.queryEvaluateByPid(id);
            if (stock==null)
            {
                model.addAttribute("msg2", "商品暂时无货");
                List<Product> products = productService.queryAllProduct();
                model.addAttribute("prducts", products);
                List<Product> products1 = productMapper.randomSelectProduct();
                model.addAttribute("products1", products1);
                return "index-2";
            }
            else
            {
                if (product.getPprice()>25.0)
                {
                    //给予10元优惠券
                    Vouchers vouchers = vouchersMapper.queryVouchersById(5);
                    Img img = imgMapper.queryImgById(id);
                    System.out.println(stock);
                    System.out.println(product);
                    System.out.println(vouchers);
                    model.addAttribute("msg", id);
                    model.addAttribute("msg1", product);
                    model.addAttribute("msg2", stock);
                    model.addAttribute("msg3", vouchers);
                    model.addAttribute("msg4", img);
                    model.addAttribute("evaluates", evaluates);
                    return "user/product/product-detail";
                }
                else
                {
                    Vouchers vouchers = vouchersMapper.queryVouchersByPrice(1);
                    System.out.println(stock);
                    Img img = imgMapper.queryImgById(id);
                    System.out.println(img);
                    model.addAttribute("msg", id);
                    model.addAttribute("msg1", product);
                    model.addAttribute("msg2", stock);
                    model.addAttribute("msg3", vouchers);
                    model.addAttribute("msg4", img);
                    model.addAttribute("evaluates", evaluates);
                    return "user/product/product-detail";
                }
            }
        }catch (NotLoginException e){
            String message = e.getMessage()+"，请登录或注册后下单";
            model.addAttribute("msg3", message);
            return "login";
        }
    }


    @RequestMapping("/productUpd")
    public String productUpd(Product product)
    {
        productService.updProduct(product);
        return "redirect:/queryAllProduct";
    }

}
